package com.bodu.system.controller;

import cn.hutool.core.util.ObjectUtil;
import com.bodu.core.common.AbstractCommonController;
import com.bodu.core.entity.ParamReq;
import com.bodu.core.entity.result.PageResult;
import com.bodu.core.entity.result.Result;
import com.bodu.global.annon.TraceLog;
import com.bodu.global.security.LoginUser;
import com.bodu.global.security.LoginUserUtil;
import com.bodu.system.entity.request.MenuRequest;
import com.bodu.system.entity.vo.MenuVo;
import com.bodu.system.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author r.guo
 * @since 2019-05-25
 */
@Api(tags = "菜单管理")
@RestController
@RequestMapping("/menu")
public class MenuController extends AbstractCommonController<MenuService, MenuRequest, MenuVo> {

    private final MenuService menuService;

    public MenuController(MenuService menuService) {
        this.menuService = menuService;
    }

    @TraceLog()
    @GetMapping("{id}")
    @ApiOperation("菜单详情")
    public Result getModel(@PathVariable("id") Long id) {
        return super.getModel(menuService, id);
    }

    @TraceLog()
    @PostMapping
    @ApiOperation("保存菜单")
    public Result save(@RequestBody MenuRequest menuRequest) {
        return super.save(menuService, menuRequest);
    }

    @TraceLog()
    @PutMapping
    @ApiOperation("更新菜单")
    public Result update(@RequestBody MenuRequest menuRequest) {
        return super.update(menuService, menuRequest);
    }

    @TraceLog()
    @PostMapping("listPage")
    @ApiOperation("菜单列表")
    public Result listPage(@RequestBody ParamReq<MenuRequest> req) {
        return super.listPage(menuService, req);
    }

    @TraceLog()
    @PostMapping("countPage")
    @ApiOperation("菜单列表-统计")
    public Result<Integer> countPage(@RequestBody ParamReq<MenuRequest> req) {
        return super.countPage(menuService, req);
    }

    @TraceLog()
    @PostMapping("page")
    @ApiOperation("菜单列表-分页数据")
    public Result<PageResult> page(@RequestBody ParamReq<MenuRequest> req) {
        return super.page(menuService, req);
    }

    /**
     * 循环Permit
     *
     * @param
     * @return
     */
    @TraceLog()
    @ApiOperation("循环Loop")
    @PostMapping("loopList")
    public Result loopList(@RequestBody MenuRequest menuRequest) {
        LoginUser loginUser = LoginUserUtil.getPrincipal();
        if (ObjectUtil.isNotEmpty(loginUser)) {
            menuRequest.setUserId(loginUser.getId());
        }
        List<MenuVo> menuVos = menuService.loopList(menuRequest);
        return Result.success(menuVos);
    }

    @TraceLog()
    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除菜单信息")
    public Result delete(@PathVariable("id") Long id) {
        return super.delete(menuService, id);
    }

    @TraceLog()
    @ApiOperation("批量删除菜单")
    @DeleteMapping("{ids}")
    public Result batchDelete(@PathVariable("ids") List<Long> ids) {
        return super.batchDelete(menuService, ids);
    }

    @Override
    public Long getLoginUserId() {
        return LoginUserUtil.getUserId();
    }
}

